package com.ymatou.app.api.service;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymt.base.YmatouBaseCall;
import com.ymttest.utils.EnvSetup;

/**
 * 团查询Call FreeMaker Template自动生成代码
 */
public class GroupListCall extends YmatouBaseCall {
	private static String opurl = "api/Group/GroupList";

	public GroupListCall() {
		super(EnvSetup.getSystemUrl("api_app"), opurl, "POST", "PARAM");
	}

	@Override
	public void addDefHeader() {
		client.addHeader("Accept", "text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	}

	/**
	 * 获取code
	 */
	public String getCode() {
		String Code = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			Code = String.valueOf(jo.get("Code"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Code;
	}

	/**
	 * 获取BCode
	 */
	public String getBCode() {
		String BCode = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			BCode = String.valueOf(jo.get("BCode"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BCode;
	}

	/**
	 * 获取Data
	 * 
	 * @return Data
	 */
	public JSONObject getData() {
		JSONObject Data = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			Data = jo.getJSONObject("Data");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Data;
	}

	/**
	 * 获取GroupList
	 */
	public int getGroupList() {
		JSONArray list = new JSONArray();
		try {
			list = getData().getJSONArray("GroupList");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list.length();
	}

	/**
	 * 去重获取查询团的TopicId
	 * 
	 * @return Data
	 */
	public int getTopicId() {
		JSONArray GroupList = null;
		int res = -1;
		List<Integer> list = new ArrayList<Integer>();
		// 获得第一层GroupList
		try {
			GroupList = getData().getJSONArray("GroupList");
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 获得TopicId放入list
		for (int i = 0; i < GroupList.length(); i++) {
			try {
				JSONObject temp = new JSONObject(GroupList.getString(i));
				list.add((Integer) temp.get("TopicId"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		// 去重生成新的list
		List<Integer> tempList = new ArrayList<Integer>();
		for (Integer i : list) {
			if (!tempList.contains(i)) {
				tempList.add(i);
			}
		}
		if (tempList.size() == 1) {
			return tempList.get(0);
		}
		return res;
	}

	/**
	 * 获取Msg
	 * 
	 * @return
	 */
	public String getMsg() {
		String Msg = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			Msg = String.valueOf(jo.get("Msg"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Msg;
	}
}